package test

import sil.Silabeador;
import java.text.ParseException;

/**
 * Ejemplos de:
 * http://www.rae.es/rae/gestores/gespub000015.nsf/(voanexos)/arch7E8694F9D6446133C12571640039A189/$FILE/Ortografia.pdf
 */
class SilabeadorTest extends GroovyTestCase
{
  def s = new sil.Silabeador()
  def slbs
  def testdata = []

  void testconversiones()
  {
    def w = 'guitarra'
    assert w == s.unformat(s.format(w))
  }
  
  void testMonosilabos()
  {
    testdata = ['sol', 'mar', 'ah', 'él', 'perl']
    testdata.each
    {
      slbs = s.silabear(it)
      assert slbs.size()==1, it
      assert slbs[0]==it, it
    }
  }
  
  void testHiatos()
  {
    def w = 'guía'
    slbs = s.silabear(w)
    assert slbs.size()==2, w
    assert slbs[0]=='guí', w
    assert slbs[1]=='a', w
  
    w = 'teatro'
    slbs = s.silabear(w)
    assert slbs.size()==3, w
    assert slbs[0]=='te', w
    assert slbs[1]=='a', w
    assert slbs[2]=='tro', w
  
    w = 'aéreo'
    slbs = s.silabear(w)
    assert slbs.size()==4, w
    assert slbs[0]=='a', w
    assert slbs[1]=='é', w
    assert slbs[2]=='re', w
    assert slbs[3]=='o', w
  
    w = 'vigía'
    slbs = s.silabear(w)
    assert slbs.size()==3, w
    assert slbs[0]=='vi', w
    assert slbs[1]=='gí', w
    assert slbs[2]=='a', w
  
    w = 'veo'
    slbs = s.silabear(w)
    assert slbs.size()==2, w
    assert slbs[0]=='ve', w
    assert slbs[1]=='o', w
  
    w = 'salías'
    slbs = s.silabear(w)
    assert slbs.size()==3, w
    assert slbs[0]=='sa', w
    assert slbs[1]=='lí', w
    assert slbs[2]=='as', w
  
    w = 'caía'
    slbs = s.silabear(w)
    assert slbs.size()==3, w
    assert slbs[0]=='ca', w
    assert slbs[1]=='í', w
    assert slbs[2]=='a', w
  
    w = 'silabear'
    slbs = s.silabear(w)
    assert slbs.size()==4, w
    assert slbs[0]=='si', w
    assert slbs[1]=='la', w
    assert slbs[2]=='be', w
    assert slbs[3]=='ar', w
  }
  
  void testTriptongos()
  {
    def w = 'amortiguáis'
    slbs = s.silabear(w)
    assert slbs.size()==4, w
    assert slbs[0]=='a', w
    assert slbs[1]=='mor', w
    assert slbs[2]=='ti', w
    assert slbs[3]=='guáis', w

    slbs = s.silabear('buey')
    assert slbs.size()==1
    assert slbs[0]=='buey'

    w = 'despreciéis'
    slbs = s.silabear(w)
    assert slbs.size()==3, w
    assert slbs[0]=='des', w
    assert slbs[1]=='pre', w
    assert slbs[2]=='ciéis', w
    
    w = 'miau'
    slbs = s.silabear(w)
    assert slbs.size()==1
    assert slbs[0]=='miau'

    w = 'limpiáis'
    slbs = s.silabear(w)
    assert slbs.size()==2
    assert slbs[0]=='lim'
    assert slbs[1]=='piáis'
   
    w = 'averigüéis'
    slbs = s.silabear(w)
    assert slbs.size()==4
    assert slbs[0]=='a'
    assert slbs[1]=='ve'
    assert slbs[2]=='ri'
    assert slbs[3]=='güéis'

    w = 'Paraguay'
    slbs = s.silabear(w)
    assert slbs.size()==3
    assert slbs[0]=='Pa'
    assert slbs[1]=='ra'
    assert slbs[2]=='guay'

    w = 'cacahuey'
    slbs = s.silabear(w)
    assert slbs.size()==3
    assert slbs[0]=='ca'
    assert slbs[1]=='ca'
    assert slbs[2]=='huey'

    w = 'apacigüéis'
    slbs = s.silabear(w)
    assert slbs.size()==4
    assert slbs[0]=='a'
    assert slbs[1]=='pa'
    assert slbs[2]=='ci'
    assert slbs[3]=='güéis'

    w = 'estudiáis'
    slbs = s.silabear(w)
    assert slbs.size()==3
    assert slbs[0]=='es'
    assert slbs[1]=='tu'
    assert slbs[2]=='diáis'
  }

  void testDiptongos()
  {
    def w = 'mierda'
    slbs = s.silabear(w)
    assert slbs.size()==2, w
    assert slbs[0]=='mier', w
    assert slbs[1]=='da', w

    w = 'adiós'
    slbs = s.silabear(w)
    assert slbs.size()==2, w
    assert slbs[0]=='a', w
    assert slbs[1]=='diós', w
  }  
  
  void testFaciles()
  {
    def w = 'casa'
    slbs = s.silabear(w)
    assert slbs.size()==2, w
    assert slbs[0]=='ca', w
    assert slbs[1]=='sa', w

    w = 'azul'
    slbs = s.silabear(w)
    assert slbs.size()==2, w
    assert slbs[0]=='a', w
    assert slbs[1]=='zul', w
  }
  void test2Consonantes()
  {
    def w = 'África'
    slbs = s.silabear(w)
    assert slbs.size()==3, w
    assert slbs[0]=='Á', w
    assert slbs[1]=='fri', w
    assert slbs[2]=='ca', w

    w = 'transformar'
    slbs = s.silabear(w)
    assert slbs.size()==3, w
    assert slbs[0]=='trans', w
    assert slbs[1]=='for', w
    assert slbs[2]=='mar', w

  }
  void testStrict()
  {
    s = new Silabeador(strict:true)
    assert s.strict
    s = new Silabeador(strict:false)
    assert !s.strict
    
    assert !('con espacios' ==~ s.pattern())
    assert !('con,comas' ==~ s.pattern())
    assert !('con2números' ==~ s.pattern())
    assert ('normal' ==~ s.pattern())
    assert ('ññññññ' ==~ s.pattern())
    assert ('tíldés' ==~ s.pattern())
    assert ('MAYÚSCULASÑ'.toLowerCase() ==~ s.pattern())
  }
  void testValidInput()
  {
    s = new Silabeador(strict: true)
    def invalid = ['con@arroba', 'con.comas', 'con espacios', 'numer4jos']
    invalid.each { w->
      try {
        s.silabear(w)
        assert false, "no ha tirado excepción para: ${w}"
      }
      catch (Exception e) {
        assert (e instanceof ParseException), 'excepción incorrecta'
        assert e.getMessage()=="esto no es una palabra: ${w}"
      }
    }
  }
  
  void testTonica()
  {
    s = new Silabeador()
    assert (s.tonica(['mo']) == 0)

    assert (s.tonica(['ár','bol']) == 0)
    assert (s.tonica(['a','vión']) == 1)
    assert (s.tonica(['a','VIÓN']) == 1)
    assert (s.tonica(['es','drú', 'ju', 'la']) == 1)
    
    assert (s.tonica(['al','ga']) == 0)
    assert (s.tonica(['al','bor']) == 1)
  }
  
  /**
   * Pilladas que han hecho los usuarios
   */
  void testPilladas()
  {
    def w = 'esternocleidomastoideo'
    slbs = s.silabear(w)
    assert slbs.size()==9, w
    assert slbs[0]=='es', w
    assert slbs[1]=='ter', w
    assert slbs[2]=='no', w
    assert slbs[3]=='clei', w
    assert slbs[4]=='do', w
    assert slbs[5]=='mas', w
    assert slbs[6]=='toi', w
    assert slbs[7]=='de', w
    assert slbs[8]=='o', w

    w = 'cuadrado'
    slbs = s.silabear(w)
    assert slbs.size()==3, w
    assert slbs[0]=='cua', w
    assert slbs[1]=='dra', w
    assert slbs[2]=='do', w

    w = 'cuadrilátero'
    slbs = s.silabear(w)
    assert slbs.size()==5, w
    assert slbs[0]=='cua', w
    assert slbs[1]=='dri', w
    assert slbs[2]=='lá', w
    assert slbs[3]=='te', w
    assert slbs[4]=='ro', w

    w = 'picaflor'
    slbs = s.silabear(w)
    assert slbs.size()==3, w
    assert slbs[0]=='pi', w
    assert slbs[1]=='ca', w
    assert slbs[2]=='flor', w

    w = 'aeroplano'
    slbs = s.silabear(w)
    assert slbs.size()==5, w
    assert slbs[0]=='a', w
    assert slbs[1]=='e', w
    assert slbs[2]=='ro', w
    assert slbs[3]=='pla', w
    assert slbs[4]=='no', w

    w = 'subrayar'
    slbs = s.silabear(w)
    assert slbs.size()==3, w
    assert slbs[0]=='sub', w
    assert slbs[1]=='ra', w
    assert slbs[2]=='yar', w

    w = 'submarino'
    slbs = s.silabear(w)
    assert slbs.size()==4, w
    assert slbs[0]=='sub', w
    assert slbs[1]=='ma', w
    assert slbs[2]=='ri', w
    assert slbs[3]=='no', w

    w = 'subir'
    slbs = s.silabear(w)
    assert slbs.size()==2, w
    assert slbs[0]=='su', w
    assert slbs[1]=='bir', w

    w = 'mubarak'
    slbs = s.silabear(w)
    assert slbs.size()==3, w
    assert slbs[0]=='mu', w
    assert slbs[1]=='ba', w
    assert slbs[2]=='rak', w
  }

  void testVocalTonica()
  {
    assert(s.vocalTonica('mas')==1)
    assert(s.vocalTonica('vión')==2)
    assert(s.vocalTonica('más')==1)
    assert(s.vocalTonica('a')==0)
    assert(s.vocalTonica('ví')==1)
    assert(s.vocalTonica('mier')==2)
  }
}
